package com.example.chat.dao

import androidx.room.*
import com.example.chat.model.MessageStatus

import com.example.chat.model.Topic
import com.example.featuresdk.dao.BasicDao
import kotlinx.coroutines.flow.Flow
import java.sql.Timestamp

@Dao
interface TopicDao : BasicDao<Topic> {
    @Query("select * from topic order by update_timestamp desc, id asc")
    fun getAll(): Flow<List<Topic>>

    @Query("select * from topic where id = :topicId")
    fun getOne(topicId: Long): Flow<Topic>

    @Query("update topic set /*count = count + 1,*/ message = :message, update_timestamp = :timestamp where id = :topicId")
    suspend fun update(topicId: Long, message:String, timestamp: Timestamp)

    @Query("update topic set count = :count where id = :topicId")
    suspend fun updateCount(topicId: Long, count: Int = 0)

    @Query("update topic set count = (select count(1) from message where status = :status and topic_id = :topicId) where id = :topicId")
    suspend fun updateNewCount(topicId: Long, status: MessageStatus = MessageStatus.New)

    @Query("delete from topic")
    suspend fun deleteAll()
}